//
//  LGDeviceOrderDetailController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2025/2/24.
//  设备管理 -- 订单详情

#import "LGDeviceOrderDetailController.h"
#import "LGOrderDetailPatientCell.h"
#import "LGDeviceOrderDetailCell.h"
#import "LGDeviceDeliverView.h"
#import "LGDeviceRecycleView.h"

@interface LGDeviceOrderDetailController ()<UITableViewDelegate,UITableViewDataSource,LGBottomButtonDelegate>
/**   */
@property (nonatomic , strong)LGBottomButtonView *bottomView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UILabel *typeLabel;
/**   */
@property (nonatomic , strong)NSDictionary *dataDic;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *state;

@end

@implementation LGDeviceOrderDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单详情";
    if ([self.type isEqualToString:@"0"]) {
        //服务包
        self.dataArray = @[
            @{
                @"title":@" 就诊人信息",
                @"superKey":@"patient",
                @"icon":@"设备管理-就诊人信息",
                @"dataArray":@[]
            },
            @{
                @"title":@" 服务包信息",
                @"superKey":@"vipRecord",
                @"icon":@"设备管理-服务包信息",
                @"dataArray":@[
                    @{@"title":@"名        称",@"key":@"vip_title",@"superkey":@""},
                    @{@"title":@"状        态",@"key":@"status_name",@"superkey":@""},
                    @{@"title":@"订单时间",@"key":@"create_time",@"superkey":@""},
                    @{@"title":@"到期时间",@"key":@"vip_date_end",@"superkey":@""},
                ]
            },
            @{
                @"title":@" 发货信息",
                @"superKey":@"",
                @"icon":@"设备管理-发货信息",
                @"dataArray":@[
                    @{@"title":@"领取设备",@"key":@"title",@"superkey":@"equipment"},
                    @{@"title":@"设备状态",@"key":@"state_name",@"superkey":@""},
                    @{@"title":@"发货时间",@"key":@"shipped_time",@"superkey":@""},
                    @{@"title":@"设备编号",@"key":@"equipment_sn",@"superkey":@""},
                    @{@"title":@"出库仓库",@"key":@"out_title",@"superkey":@""},
                    @{@"title":@"领取方式",@"key":@"receipt_type_name",@"superkey":@""},
                    @{@"title":@"申领时间",@"key":@"received_time",@"superkey":@""},
                    @{@"title":@"押金状态",@"key":@"pay_state_name",@"superkey":@""},
                    @{@"title":@"押        金",@"key":@"deposit",@"superkey":@"equipment"},
                ]
            },
            @{
                @"title":@" 回收信息",
                @"superKey":@"",
                @"icon":@"设备管理-回收信息",
                @"dataArray":@[
                    @{@"title":@"设备状态",@"key":@"state_name",@"superkey":@""},
                    @{@"title":@"寄回时间",@"key":@"returned_time",@"superkey":@""},
                    @{@"title":@"寄回物流",@"key":@"returned_waybill_sn",@"superkey":@""},
                    @{@"title":@"入库时间",@"key":@"putstorage_time",@"superkey":@""},
                    @{@"title":@"入库仓库",@"key":@"in_title",@"superkey":@""},
                    @{@"title":@"备        注",@"key":@"remarks",@"superkey":@""},
                ]
            },
        ];
    }else{
        //商城
        self.dataArray = @[
            @{
                @"title":@" 就诊人信息",
                @"superKey":@"patient",
                @"icon":@"设备管理-就诊人信息",
                @"dataArray":@[]
            },
            @{
                @"title":@"商城订单信息",
                @"superKey":@"",
                @"icon":@"设备管理-服务包信息",
                @"dataArray":@[
                    @{@"title":@"商品名称",@"key":@"goods_name",@"superkey":@"goods"},
                    @{@"title":@"订单状态",@"key":@"pay_state_name",@"superkey":@""},
                    @{@"title":@"退款状态",@"key":@"refund_state_name",@"superkey":@""},
                    @{@"title":@"订单时间",@"key":@"create_time",@"superkey":@""},
                ]
            },
            @{
                @"title":@" 发货信息",
                @"superKey":@"",
                @"icon":@"设备管理-发货信息",
                @"dataArray":@[
                    @{@"title":@"设备状态",@"key":@"deliver_status_name",@"superkey":@""},
                    @{@"title":@"发货时间",@"key":@"ship_time",@"superkey":@""},
                    @{@"title":@"设备编号",@"key":@"goods_no",@"superkey":@""},
                    @{@"title":@"出库仓库",@"key":@"out_title",@"superkey":@""},
                    @{@"title":@"领取方式",@"key":@"receipt_type_name",@"superkey":@""},
                ]
            },
            @{
                @"title":@" 回收信息",
                @"superKey":@"",
                @"icon":@"设备管理-回收信息",
                @"dataArray":@[
                    @{@"title":@"设备状态",@"key":@"deliver_status_name",@"superkey":@""},
                    @{@"title":@"入库时间",@"key":@"finish_time",@"superkey":@""},
                    @{@"title":@"入库仓库",@"key":@"in_title",@"superkey":@""},
                ]
            },
        ];
    }
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    [self requestData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:@"DeviceRefreshService" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:@"DeviceRefreshShope" object:nil];
}


-(void)requestData{
    NSString *action = [self.type isEqualToString:@"0"]?@"/doctor/equipment.equipment/receiptDetail":@"/doctor/equipment.equipment/mallDetail";
    NSDictionary *paramaDic = [self.type isEqualToString:@"0"]?@{@"receipt_id":self.recordid}:@{@"record_id":self.recordid};
    [RequestUtil withPOST:action parameters:paramaDic withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.dataDic = returnData;
            [self.tableView reloadData];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.tableView.frame = CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(78));
    if ([self.type isEqualToString:@"0"]) {
        self.typeLabel.text = [NSString stringWithFormat:@"订单类型：%@",dataDic[@"give_type_name"]];
        //10|待领取 20|待发货 30|已发货 40|已寄出 50|已入库 101|已取消 102|已赠送 103|待归还 104|回收失败
        self.state = LGNSString(dataDic[@"state"]);
        NSString *serviceState = LGNSString(dataDic[@"vipRecord"][@"status_name"]);
        //3-设备赠送 、 4-设备租用 、 8-设备租用[需押金] 、 6-长程心电监测到家服务
        NSString *vipService = LGNSString(dataDic[@"vip_service_type"]);
        if ([vipService isEqualToString:@"3"] && [self.state isEqualToString:@"103"] && [serviceState  isEqualToString:@"已退款"] ) {
            //设备赠送类型下 -- 已退款+待回收 才显示设备回收按钮
            self.bottomView.hidden = NO;
            [self.bottomView.button1 setTitle:@"设备回收" forState:UIControlStateNormal];
            
        }else if (([self.state isEqualToString:@"10"] || [self.state isEqualToString:@"20"]) && ([serviceState  isEqualToString:@"生效中"] || [serviceState isEqualToString:@"未生效"])) {
            self.bottomView.hidden = NO;
            [self.bottomView.button1 setTitle:@"设备发货" forState:UIControlStateNormal];
            
        }else if (([self.state isEqualToString:@"30"] || [self.state isEqualToString:@"40"] || [self.state isEqualToString:@"103"]) && ([serviceState  isEqualToString:@"生效中"] || [serviceState  isEqualToString:@"已到期"] || [serviceState isEqualToString:@"已退款"]) && ![vipService isEqualToString:@"3"]){
            self.bottomView.hidden = NO;
            [self.bottomView.button1 setTitle:@"设备回收" forState:UIControlStateNormal];
            
        }else{
            self.bottomView.hidden = YES;
            self.tableView.frame = CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight);
        }
    }else{
        self.typeLabel.text = [NSString stringWithFormat:@"订单类型：%@",@"商城购买"];
        //state:0-待发货 、 2-已发货 、3-待回收 、9-回收失败 、101-已入库
        NSString *state = LGNSString(dataDic[@"deliver_status"]);
        self.state = state;
        //0-待支付 、 10-支付中 、 100-已支付 、 101-已取消 、 102-支付超时
        NSString *payState = LGNSString(dataDic[@"pay_state"]);
        //0-未退款 、 10-退款中 、 90-部分退款 、 100-已退款 、 101-退款失败
        NSString *refundState = LGNSString(dataDic[@"refund_state"]);
        //显示设备发货按钮
        if ([payState isEqualToString:@"100"] && [state isEqualToString:@"0"] && [refundState  isEqualToString:@"0"]) {
            self.bottomView.hidden = NO;
            [self.bottomView.button1 setTitle:@"设备发货" forState:UIControlStateNormal];
            
        }else if ([refundState isEqualToString:@"100"] && ([state isEqualToString:@"2"] || [state isEqualToString:@"3"])){
            self.bottomView.hidden = NO;
            [self.bottomView.button1 setTitle:@"设备回收" forState:UIControlStateNormal];
            
        }else{
            self.bottomView.hidden = YES;
            self.tableView.frame = CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight);
        }
    }
}



//设备发货 、 设备回收
-(void)bottomButtonTouchedWithTitle:(NSString *)title{
    if ([title isEqualToString:@"设备发货"]) {
        LGDeviceDeliverView *deliverView = [[LGDeviceDeliverView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        deliverView.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.8];
        deliverView.type = self.type;
        deliverView.dataDic = self.dataDic;
        [LGKeyWindow addSubview:deliverView];
    }else{
        LGDeviceRecycleView *recycleView = [[LGDeviceRecycleView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        recycleView.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.8];
        recycleView.type = self.type;
        recycleView.dataDic = self.dataDic;
        [LGKeyWindow addSubview:recycleView];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if ([self.type isEqualToString:@"0"]&&([self.state isEqualToString:@"105"]||[self.state isEqualToString:@"103"]||[self.state isEqualToString:@"40"]||[self.state isEqualToString:@"50"]||[self.state isEqualToString:@"104"])) {
        //服务包
        return 4;
    }else if ([self.type isEqualToString:@"1"]&&([self.state isEqualToString:@"3"]||[self.state isEqualToString:@"9"]||[self.state isEqualToString:@"101"]||[self.state isEqualToString:@"105"])){
        //商城
        return 4;
    }
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        NSArray *tempArray = self.dataArray[section][@"dataArray"];
        return tempArray.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        LGOrderDetailPatientCell *patientCell = [tableView dequeueReusableCellWithIdentifier:@"patientCell" forIndexPath:indexPath];
        patientCell.selectionStyle = UITableViewCellSelectionStyleNone;
        patientCell.dataDic = self.dataDic[@"patient"];
        return patientCell;
        
    }else{
        NSArray *tempArray = self.dataArray[indexPath.section][@"dataArray"];
        NSString *superKey = LGNSString(self.dataArray[indexPath.section][@"superKey"]);
        LGDeviceOrderDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dataDic = tempArray[indexPath.row];
        cell.resultDic = (superKey.length>0)?self.dataDic[superKey]:self.dataDic;
        return cell;
    }
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(50);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGCornerHeaderView *headView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(50))];
    headView.topMargin = viewPix(10);
    UIButton *titleBtn = [UIButton buttonWithTitle:self.dataArray[section][@"title"] titleFont:LGFontWeight(18, UIFontWeightMedium) textColor:@"#282828" imageName:self.dataArray[section][@"icon"]];
    titleBtn.frame = CGRectMake(viewPix(32), viewPix(20), Screen_W-viewPix(100), viewPix(20));
    titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    titleBtn.userInteractionEnabled = NO;
    [headView addSubview:titleBtn];
    
    return headView;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(15);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
    footView.bottomMargin = viewPix(5);
    return footView;
}


-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGOrderDetailPatientCell class] forCellReuseIdentifier:@"patientCell"];
        [_tableView registerClass:[LGDeviceOrderDetailCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [self tableHeaderView];
    }
    return _tableView;
}

-(UILabel *)typeLabel{
    if(!_typeLabel){
        _typeLabel = [UILabel lableWithText:@"订单类型：" colorString:@"#282828" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _typeLabel;
}

-(LGBottomButtonView *)bottomView{
    if(!_bottomView){
        _bottomView = [[LGBottomButtonView alloc]initWithTitle:@"设备发货" color:@"#4C87FD" type:LGBottomButtonTypeBack frame:CGRectMake(0, Screen_H-topBarHeight-viewPix(78), Screen_W, viewPix(78))];
        _bottomView.cornerRidus = viewPix(23);
        _bottomView.delegate = self;
    }
    return _bottomView;
}

-(UIView *)tableHeaderView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(40))];
    baseView.backgroundColor = [UIColor clearColor];
    
    UIView *iconView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(14), 4, viewPix(12))];
    iconView.backgroundColor = [UIColor colorWithString:@"#77D4C6"];
    [baseView addSubview:iconView];
    [baseView addSubview:self.typeLabel];
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconView.mas_right).offset(viewPix(12));
        make.centerY.equalTo(iconView);
    }];
    
    return baseView;
}

@end
